package com.zhima.controller.plain;

import com.zhima.annotation.MustLogin;
import com.zhima.dto.CreateQuestionDTO;
import com.zhima.dto.UpdateTestCaseTemplateDTO;
import com.zhima.dto.ZhimaOjPageResult;
import com.zhima.dto.ZhimaOjResult;
import com.zhima.entity.OjQuestion;
import com.zhima.enums.QuestionScope;
import com.zhima.properties.ZhimaQuestionProperties;
import com.zhima.service.OjQuestionService;
import com.zhima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static com.zhima.dto.ZhimaOjResult.success;

/**
 * <p>
 * 操作用户自己的题目，都是需要检查登录的
 * </p>
 *
 * @author zhima
 * @since 2023-08-25
 */
@RestController
@RequestMapping("/question")
@MustLogin
public class OjQuestionController {
    @Autowired
    private OjQuestionService ojQuestionService;
    @Autowired
    private ZhimaQuestionProperties zhimaQuestionProperties;

    /**
     * 用户查自己现在出了哪些题目（未提交审核）
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryQuestionBasicInfo(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return ZhimaOjPageResult.success(ojQuestionService.pageQueryQuestionBasicInfo(UserHolder.get().getId(), pageNum, pageSize));
    }

    /**
     * 返回指定题目详细信息
     *
     * @param questionId
     * @return
     */
    @GetMapping("/{questionId}")
    @MustLogin
    public ZhimaOjResult queryQuestionDetailById(@PathVariable("questionId") Long questionId) {
        return success(ojQuestionService.queryQuestionDetailById(UserHolder.get().getId(), questionId));
    }

    @GetMapping("/edit/{questionId}")
    public ZhimaOjResult queryQuestionEditById(@PathVariable("questionId") Long questionId) {
        return success(ojQuestionService.queryQuestionEditById(UserHolder.get().getId(), questionId));
    }

    /**
     * 删除用户的指定题目
     *
     * @param questionId
     * @return
     */
    @DeleteMapping("/{questionId}")
    public ZhimaOjResult deleteQuestionById(@PathVariable("questionId") Long questionId) {
        return success(ojQuestionService.deleteQuestionById(UserHolder.get().getId(), questionId));
    }

    /**
     * 创建默认前缀下的题目
     * 所有的普通用户创建的题目都是在默认前缀下的
     *
     * @param createQuestionDTO
     * @return
     */
    @PostMapping("/new")
    public ZhimaOjResult createNewQuestion(@RequestBody CreateQuestionDTO createQuestionDTO) {
        OjQuestion newQuestion = ojQuestionService.createNewQuestion(UserHolder.get(), createQuestionDTO, createQuestionDTO.getPrefix() == null ? zhimaQuestionProperties.getUserQuestionDefaultPrefix() : createQuestionDTO.getPrefix());
        return success(newQuestion.getId().toString());
    }

    /**
     * 补充题目基本信息，不涉及测试用例信息的修改
     *
     * @param createQuestionDTO
     * @return
     */
    @PutMapping
    public ZhimaOjResult supplyDetail(@RequestBody CreateQuestionDTO createQuestionDTO) {
        return success(ojQuestionService.supplyDetail(UserHolder.get().getId(), createQuestionDTO));
    }

    /**
     * 修改可见性不需要审核，直接更新三张表（question、question_audit、question_publish）的可见性
     *
     * @param questionId
     * @param questionScope
     * @return
     */
    @PostMapping("/scope/{questionId}/{questionScope}")
    public ZhimaOjResult updateQuestionScope(@PathVariable("questionId") Long questionId, @PathVariable("questionScope") QuestionScope questionScope) {
        return success(ojQuestionService.updateQuestionScope(UserHolder.get().getId(), questionId, questionScope));
    }

    /**
     * 用户上传测试用例
     *
     * @param file
     * @param questionId
     * @return
     * @throws IOException
     */
    @PostMapping("/test-case/{questionId}")
    public ZhimaOjResult updateTestCases(@RequestBody MultipartFile file, @PathVariable("questionId") Long questionId) throws IOException {
        return success(ojQuestionService.updateTestCases(UserHolder.get().getId(), file.getInputStream(), questionId));
    }

    /**
     * 获得测试模板信息
     *
     * @param questionId
     * @return
     */
    @GetMapping("/test-case-template/{questionId}")
    public ZhimaOjResult getTestCasesTemplate(@PathVariable("questionId") Long questionId) {
        return success(ojQuestionService.getTestCasesTemplate(questionId));
    }

    @PostMapping("/test-case-template/{questionId}")
    public ZhimaOjResult updateTestCasesTemplate(@PathVariable("questionId") Long questionId, @RequestBody UpdateTestCaseTemplateDTO updateTestCaseTemplateDTO) {
        return success(ojQuestionService.updateTestCasesTemplate(questionId, updateTestCaseTemplateDTO));
    }

    /**
     * 批量上传题目
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/batch")
    public ZhimaOjResult batchImportQuestions(@RequestBody MultipartFile file) throws IOException {
        // file是压缩包
        return success(ojQuestionService.batchImportQuestions(file.getInputStream()));
    }

    @GetMapping("/batch/info")
    public ZhimaOjResult batchImportQuestionInfos() {
        return success(ojQuestionService.batchImportQuestionInfos(UserHolder.get().getId()));
    }

    @DeleteMapping("/batch/info/{questionName}")
    public ZhimaOjResult deleteBatchQuestionInfo(@PathVariable("questionName") String questionName) {
        return success(ojQuestionService.deleteBatchQuestionInfo(UserHolder.get().getId(), questionName));
    }
}
